package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.useraccount.facede.TencentUploadFileComponent;
import com.parking.mgr.useraccount.model.entity.ParkingRecordFileEntity;
import com.parking.mgr.useraccount.model.entity.ParkingRecordInfoEntity;
import com.parking.mgr.useraccount.model.mapper.ParkingRecordFileMapper;
import com.parking.mgr.useraccount.model.vo.ParkingRecordFileVO;
import com.parking.mgr.useraccount.service.ParkingRecordFileService;
import com.parking.mgr.useraccount.service.ParkingRecordInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ParkingRecordFileServiceImpl
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/3/6
 */
@Service
@Slf4j
public class ParkingRecordFileServiceImpl extends ServiceImpl<ParkingRecordFileMapper, ParkingRecordFileEntity> implements ParkingRecordFileService {

    @Resource
    private TencentUploadFileComponent tencentUploadFileComponent;

    @Resource
    private ParkingRecordInfoService parkingRecordInfoService;

    @Override
    public ParkingRecordFileEntity getOneByParkingRecordId(String fkParkingRecordId) {

        LambdaQueryWrapper<ParkingRecordFileEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordFileEntity>()
                .eq(ParkingRecordFileEntity::getFkParkingRecordId, fkParkingRecordId);

        return this.getOne(queryWrapper);
    }

    @Override
    public void uploadApplicationFormFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String applicationFormUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFApplicationFormId(IdUtils.getId());
            parkingRecordFileEntity.setFApplicationFormUrl(applicationFormUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFApplicationFormId(IdUtils.getId());
            newParkingRecordFileEntity.setFApplicationFormUrl(applicationFormUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadManagementSystemFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String managementSystemUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFManagementSystemId(IdUtils.getId());
            parkingRecordFileEntity.setFManagementSystemUrl(managementSystemUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFManagementSystemId(IdUtils.getId());
            newParkingRecordFileEntity.setFManagementSystemUrl(managementSystemUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadUseCertificationFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String legalCardFrontUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFUseCertificationId(IdUtils.getId());
            parkingRecordFileEntity.setFUseCertificationUrl(legalCardFrontUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFUseCertificationId(IdUtils.getId());
            newParkingRecordFileEntity.setFUseCertificationUrl(legalCardFrontUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadFireCertificateFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String legalCardBackUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFFireCertificateId(IdUtils.getId());
            parkingRecordFileEntity.setFFireCertificateUrl(legalCardBackUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFFireCertificateId(IdUtils.getId());
            newParkingRecordFileEntity.setFFireCertificateUrl(legalCardBackUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadIdCardEntrustFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String transactorCardFrontUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFIdCardEntrustFileId(IdUtils.getId());
            parkingRecordFileEntity.setFIdCardEntrustFileUrl(transactorCardFrontUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFIdCardEntrustFileId(IdUtils.getId());
            newParkingRecordFileEntity.setFIdCardEntrustFileUrl(transactorCardFrontUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadParkingDiagramEnvFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String transactorCardBackUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFParkingDiagramEnvId(IdUtils.getId());
            parkingRecordFileEntity.setFParkingDiagramEnvUrl(transactorCardBackUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFParkingDiagramEnvId(IdUtils.getId());
            newParkingRecordFileEntity.setFParkingDiagramEnvUrl(transactorCardBackUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadCompletionAcceptRecordFile(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String entrustFileUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFCompletionAcceptRecordId(IdUtils.getId());
            parkingRecordFileEntity.setFCompletionAcceptRecordUrl(entrustFileUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFCompletionAcceptRecordId(IdUtils.getId());
            newParkingRecordFileEntity.setFCompletionAcceptRecordUrl(entrustFileUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadProcessPlan(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String processPlanUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFProcessPlanId(IdUtils.getId());
            parkingRecordFileEntity.setFProcessPlanUrl(processPlanUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFProcessPlanId(IdUtils.getId());
            newParkingRecordFileEntity.setFProcessPlanUrl(processPlanUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public void uploadChargePublicity(MultipartFile file, String fkParkingRecordId, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(fkParkingRecordId);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        String chargePublicityUrl = tencentUploadFileComponent.uploadFile(file);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOneByParkingRecordId(fkParkingRecordId);

        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {
            parkingRecordFileEntity.setFChargePublicityId(IdUtils.getId());
            parkingRecordFileEntity.setFChargePublicityUrl(chargePublicityUrl);
            this.updateById(parkingRecordFileEntity);
        } else {
            ParkingRecordFileEntity newParkingRecordFileEntity = new ParkingRecordFileEntity();
            newParkingRecordFileEntity.setFChargePublicityId(IdUtils.getId());
            newParkingRecordFileEntity.setFChargePublicityUrl(chargePublicityUrl);
            newParkingRecordFileEntity.setFkParkingRecordId(fkParkingRecordId);
            newParkingRecordFileEntity.setId(IdUtils.getId());
            this.save(newParkingRecordFileEntity);
        }
    }

    @Override
    public Map<String, Object> getUrl(String fkParkingRecordId) {

        LambdaQueryWrapper<ParkingRecordFileEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordFileEntity>()
                .eq(ParkingRecordFileEntity::getFkParkingRecordId, fkParkingRecordId);

        ParkingRecordFileEntity parkingRecordFileEntity = this.getOne(queryWrapper);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (ObjectUtil.isNotNull(parkingRecordFileEntity)) {

            ParkingRecordFileVO application = new ParkingRecordFileVO();
            application.setTitle("停车场审批申请表");
            application.setUrl(parkingRecordFileEntity.getFApplicationFormUrl());
            resultMap.put("application", application);

            ParkingRecordFileVO management = new ParkingRecordFileVO();
            management.setTitle("停车场管理制度");
            management.setUrl(parkingRecordFileEntity.getFManagementSystemUrl());
            resultMap.put("management", management);

            ParkingRecordFileVO certification = new ParkingRecordFileVO();
            certification.setTitle("有效的实地使用证明材料");
            certification.setUrl(parkingRecordFileEntity.getFUseCertificationUrl());
            resultMap.put("certification", certification);

            ParkingRecordFileVO fire = new ParkingRecordFileVO();
            fire.setTitle("消防合格证明");
            fire.setUrl(parkingRecordFileEntity.getFFireCertificateUrl());
            resultMap.put("fire", fire);

            ParkingRecordFileVO entrust = new ParkingRecordFileVO();
            entrust.setTitle("法人、责任人、办理人身份证明、企业授权经办人委托书");
            entrust.setUrl(parkingRecordFileEntity.getFIdCardEntrustFileUrl());
            resultMap.put("entrust", entrust);

            ParkingRecordFileVO diagram = new ParkingRecordFileVO();
            diagram.setTitle("车场平面示意图及现状环境照片");
            diagram.setUrl(parkingRecordFileEntity.getFParkingDiagramEnvUrl());
            resultMap.put("diagram", diagram);

            ParkingRecordFileVO completion = new ParkingRecordFileVO();
            completion.setTitle("竣工验收备案表");
            completion.setUrl(parkingRecordFileEntity.getFCompletionAcceptRecordUrl());
            resultMap.put("completion", completion);

            ParkingRecordFileVO process = new ParkingRecordFileVO();
            process.setTitle("车场应急预案");
            process.setUrl(parkingRecordFileEntity.getFProcessPlanUrl());
            resultMap.put("process", process);

            ParkingRecordFileVO charge = new ParkingRecordFileVO();
            charge.setTitle("停车场收费公示牌");
            charge.setUrl(parkingRecordFileEntity.getFChargePublicityUrl());
            resultMap.put("charge", charge);
        }


        return resultMap;
    }
}
